package org.example.buffer;

import java.nio.IntBuffer;

/**
 * 只读
 *
 * @author 欧阳劲驰
 * @since JDK1.8
 */
public class Demo3 {
    public static void main(String[] args) {
        //创建缓冲区
        IntBuffer intBuffer = IntBuffer.allocate(5);
        //写入缓冲区
        intBuffer.put(1);
        //翻转
        intBuffer.flip();
        //获取只读缓冲区
        IntBuffer readOnlyBuffer = intBuffer.asReadOnlyBuffer();
        //读取缓冲区
        System.out.println(readOnlyBuffer.get());
        //清理
        readOnlyBuffer.clear();
        //对只读缓冲区写入,此时会报错
        readOnlyBuffer.put(2);
    }
}
